/**
 * @author skoon
 */
var webupdaterbridge = function(){

    var prepareStatement = function(sqlStatement){
    
        function createResult(event){
            air.trace("statement executed");
        }
        
        function createError(event){
            air.trace("Error code:", event.error.code);
            air.trace("Details:", event.error.message);
        }
        
        var statement = new air.SQLStatement();
        
        statement.sqlConnection = webupdaterbridge.currentConnection;
        statement.text = sqlStatement;
        
        statement.addEventListener(air.SQLEvent.RESULT, createResult);
        statement.addEventListener(air.SQLErrorEvent.ERROR, createError);
        
        return statement;
    };
    
    return {
        SaveSiteInfo: webupdaterbridge.SaveSiteInfo = function(siteName, userName, password){
            air.trace("saving site info " + siteName + " " + userName + " " + password)
        
            var CheckForSiteSql = "Select 1 from siteinfo where siteName = '" + siteName + "'";
            var CheckForSiteStatement = prepareStatement(CheckForSiteSql);
            air.trace(CheckForSiteSql);
            
            CheckForSiteStatement.execute();
            var siteResult = CheckForSiteStatement.getResult().data;
            
            if (null == siteResult) {
                var insertSiteinfoSQL = "insert into siteinfo(siteName,username,password) " +
                "values('" +
                siteName +
                "','" +
                userName +
                "','" +
                password +
                "')";
                var insertStatement = prepareStatement(insertSiteinfoSQL);
                air.trace(insertSiteinfoSQL);
                insertStatement.execute();
            }
            else {
                var updateSiteInfoSQL = "update siteinfo set username = '" + userName + "'" +
                ",password = '" +
                password +
                "' where sitename = '" +
                siteName +
                "'";
                var updateStatement = prepareStatement(updateSiteInfoSQL);
                air.trace(updateSiteInfoSQL);
                updateStatement.execute();
            }
        },
        init: "",
        trace: function(message){
            air.trace(message);
        },
        childBridge: "",
        currentConnection: "",
		getUsernameForSite: function(siteName) {
			var sql = "SELECT username from SiteInfo where sitename = :sitename";
			var getUsernameStatment = prepareStatement(sql);
			getUsernameStatment.parameters[':sitename'] = siteName;
			var result = getUsernameStatment.execute();
			return result.data;
		}
    }
}



webupdaterbridge.init = function(){
    air.trace("webupdaterbridge init");
    document.getElementById("RootBridge").contentWindow.parentSandboxBridge = webupdaterbridge();
    webupdaterbridge.childBridge = document.getElementById("RootBridge").contentWindow.childSandboxBridge;
    //initialize the connection
    air.trace("opening connection")
    webupdaterbridge.currentConnection = new air.SQLConnection( true );
    var fileHandle = air.File.applicationStorageDirectory.resolvePath("webupdater.db");
    webupdaterbridge.currentConnection.open(fileHandle, true);

    //create the tables if necessary
    var createSiteinfoTableSQL = "CREATE TABLE IF NOT EXISTS siteinfo (siteId INTEGER PRIMARY KEY AUTOINCREMENT," +
    " siteName text, userName text, password text)";
    air.trace(createSiteinfoTableSQL);
    var siteInfoStatement = new air.SQLStatement();
    siteInfoStatement.sqlConnection = webupdaterbridge.currentConnection;
    siteInfoStatement.text = createSiteinfoTableSQL;
	siteInfoStatement.execute();
                
    webupdaterbridge.childBridge.init();
}
